<?php

/**
 * Description of JcApp
 *
 * @author yuanjun.liu
 */
class JcApp extends App {

    public $pageSize = 25;
    protected $secretKey = 'JC4E0F3A2WB9B2N';

    protected function getTableName() {
        return 'app';
    }

    protected function getPrimaryField() {
        return 'id';
    }

    protected function isAutoIncrement() {
        return false;
    }

    public function showAdd() {
        include $this->tpl('appAdd');
    }

    public function doAdd() {
        ($appName = (string) trim($_REQUEST['name'])) || jaError(5);
        $appId = strtoupper(uniqid('JC'));
        if ($commentTable = (string) trim($_REQUEST['comment_table'])) {
            //验证表是否存在
        } else {
            $commentTable = 'comment';
        }
        if ($textTable = (string) trim($_REQUEST['text_table'])) {
            //验证表是否存在
        } else {
            $textTable = 'text';
        }
        $appData = array(
            'id' => $appId,
            'key' => md5($appId . $this->secretKey),
            'name' => $appName,
            'description' => (string) trim($_REQUEST['description']),
            'comment_table' => $commentTable,
            'text_table' => $textTable,
            'comment_count' => 0,
            'access_count' => 0,
            'create_time' => TS_NOW
        );
        if ($this->table()->insert($appData)) {
            $this->redirect(jaUrl('app', 'list'), '添加成功！正在跳回列表页..', 1, 2);
        } else {
            $this->redirect(jaUrl('app', 'list'), '添加失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
        }
    }

    public function showEdit() {
        $id = (string) $_REQUEST['id'];
        ($detail = $this->find($id)) || jaError(6);
        include $this->tpl('appEdit');
    }

    public function doEdit() {
        ($id = (string) trim($_REQUEST['id'])) || jaError(6);
        ($appName = (string) trim($_REQUEST['name'])) || jaError(5);
        if ($commentTable = (string) trim($_REQUEST['comment_table'])) {
            //验证表是否存在
        } else {
            $commentTable = 'comment';
        }
        if ($textTable = (string) trim($_REQUEST['text_table'])) {
            //验证表是否存在
        } else {
            $textTable = 'text';
        }
        $appData = array(
            'name' => $appName,
            'description' => (string) trim($_REQUEST['description']),
            'comment_table' => $commentTable,
            'text_table' => $textTable
        );
        if ($_REQUEST['change_key'])
            $appData['key'] = md5($appId . $this->secretKey . uniqid());
        if ($this->table()->where(array($this->getPrimaryField(), 'eq', $id))->update($appData)) {
            $this->redirect(jaUrl('app', 'list'), '更新成功！正在跳回列表页..', 1, 2);
        } else {
            $this->redirect(jaUrl('app', 'list'), '操作失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
        }
    }

    public function doDelete() {
        ($id = (string) trim($_REQUEST['id'])) || jaError(6);
        if ($this->delete($id)) {
            $this->redirect(jaUrl('app', 'list'), '删除成功！正在跳回列表页..', 1, 2);
        } else {
            $this->redirect(jaUrl('app', 'list'), '操作失败！错误信息如下:<br />' . $this->db->getError(), 2, 0);
        }
    }

    public function showList() {
        (isset($_REQUEST['page']) && $page = (int) $_REQUEST['page']) || ($page = 1);
        $total = $this->count();
        $list = $this->table()->limit(($page - 1) * $this->pageSize . ',' . $this->pageSize)->order('`create_time` DESC')->select();
        $showPages = showPages($total, $this->pageSize);
        include $this->tpl('appList');
    }

}

?>
